package com.ccx.library.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.UpdateChainWrapper;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;

public class WrapperUtil<T> {

    public static<T> QueryWrapper Query(T obj){
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        Field[] fields = obj.getClass().getDeclaredFields();
        Field.setAccessible(fields,true);
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            Object o = null;
            try {
                o = fields[i].get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (!ObjectUtils.isEmpty(o)){
                queryWrapper.eq(name,o);
            }
        }
        return queryWrapper;
    }

    public static <T> UpdateWrapper Update(T obj,String fieldIdName,String id){
        UpdateWrapper<Object> updateWrapper = new UpdateWrapper<>();
        System.out.println(id);
        updateWrapper.eq(fieldIdName,id);
        Field[] fields = obj.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            Object o = null;
            try {
                fields[i].setAccessible(true);
                o = fields[i].get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
//            if (fieldIdName.equals(name)){
//
//            }

            if (!ObjectUtils.isEmpty(o) && !fieldIdName.equals(name)){
                String newId = fieldIdName.replace("_", "");
                if (!name.equalsIgnoreCase(newId)){
                    updateWrapper.set(name,o);
                }
            }
        }
        return updateWrapper;
    }

    public static <T> QueryWrapper Search(T obj){
        QueryWrapper<T> Wrapper = new QueryWrapper<>();
        Field[] fields = obj.getClass().getDeclaredFields();
        Field.setAccessible(fields,true);
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            Object o = null;
            try {
                o = fields[i].get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (!ObjectUtils.isEmpty(o)){
                Wrapper.like(name,o);
                Wrapper.orderByDesc("create_time");
            }
        }
        return Wrapper;
    }

    /**
     * 倒序
     */
    public static QueryWrapper orderDesc(Class c){
        Object instance = null;
        try {
            instance = c.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(ModelVoIdUtil.getTime(instance));
        return queryWrapper;
    }
}
